<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 400px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            color: #555;
        }
        input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 12px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 10px;
            border-radius: 4px;
            display: none;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .register-link {
            text-align: center;
            margin-top: 20px;
        }
        .register-link a {
            color: #007bff;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>登录测试</h1>
        
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" id="username" name="username" required>
            </div>
            
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" name="password" required>
            </div>
            
            <button type="submit">登录</button>
        </form>
        
        <div id="result" class="result"></div>
        
        <div class="register-link">
            <a href="#" onclick="showRegisterForm()">没有账号？点击注册</a>
        </div>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            try {
                const response = await fetch('/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ username, password })
                });
                
                const data = await response.json();
                const resultDiv = document.getElementById('result');
                
                if (data.success) {
                    resultDiv.className = 'result success';
                    resultDiv.textContent = `登录成功！欢迎 ${data.user.username}`;
                } else {
                    resultDiv.className = 'result error';
                    resultDiv.textContent = data.message;
                }
                
                resultDiv.style.display = 'block';
                
            } catch (error) {
                console.error('登录错误:', error);
                const resultDiv = document.getElementById('result');
                resultDiv.className = 'result error';
                resultDiv.textContent = '网络错误，请稍后重试';
                resultDiv.style.display = 'block';
            }
        });

        function showRegisterForm() {
            const username = prompt('请输入用户名:');
            const password = prompt('请输入密码:');
            const email = prompt('请输入邮箱:');
            
            if (username && password && email) {
                registerUser(username, password, email);
            }
        }

        async function registerUser(username, password, email) {
            try {
                const response = await fetch('/auth/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ username, password, email })
                });
                
                const data = await response.json();
                const resultDiv = document.getElementById('result');
                
                if (data.success) {
                    resultDiv.className = 'result success';
                    resultDiv.textContent = '注册成功！现在可以登录了';
                } else {
                    resultDiv.className = 'result error';
                    resultDiv.textContent = data.message;
                }
                
                resultDiv.style.display = 'block';
                
            } catch (error) {
                console.error('注册错误:', error);
                const resultDiv = document.getElementById('result');
                resultDiv.className = 'result error';
                resultDiv.textContent = '注册失败，请稍后重试';
                resultDiv.style.display = 'block';
            }
        }
    </script>
</body>
</html> 